<?php
/**
 * 策略模式
 *
 * 针对一组算法，将每一种算法封装到拥有公共接口的独立类中，可以达成呼唤。
 */


//抽象策略角色，通常由一个接口或抽象类实现。此角色给出所有具体策略类
//所需的接口
abstract class Strategy 
{
	abstract public function AlgorithmInterface();
}
//具体策略角色A
class ConcreteStrategyA extends Strategy{
	//算法A
	public function AlgorithmInterface()
	{
		echo "ConcreteStrategyA AlgorithmInterface".PHP_EOL;
	}
}
//具体策略角色B
class ConcreteStrategyB extends Strategy{
	//算法B
	public function AlgorithmInterface()
	{
		echo "ConcreteStrategyB AlgorithmInterface".PHP_EOL;
	}
}
//具体策略角色C
class ConcreteStrategyC extends Strategy{
	//算法B
	public function AlgorithmInterface()
	{
		echo "ConcreteStrategyB AlgorithmInterface".PHP_EOL;
	}
}
//环境角色
class Context{
	//算法类的引用
	private $strategy;
	//获取算法
	public function __construct(Strategy $strategy)
	{
		$this->strategy = $strategy;
	}
	//执行算法
	public function ContextInterface()
	{
		$this->strategy->AlgorithmInterface();
	}
}

//d环境执行算法A
$d = new Context(new ConcreteStrategyA());
$d->ContextInterface();
//e环境执行算法B
$e = new Context(new ConcreteStrategyB());
$e->ContextInterface();
//f环境执行算法C
$f = new Context(new ConcreteStrategyC());
$f->ContextInterface();